package com.woniuxy.project115.kaer.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.platform.dto.RolePermissionDto;
import com.woniuxy.project115.kaer.platform.entity.Permission;
import com.woniuxy.project115.kaer.platform.entity.Role;
import com.woniuxy.project115.kaer.platform.entity.RolePermission;
import com.woniuxy.project115.kaer.platform.mapper.PermissionMapper;
import com.woniuxy.project115.kaer.platform.mapper.RoleMapper;
import com.woniuxy.project115.kaer.platform.mapper.RolePermissionMapper;
import com.woniuxy.project115.kaer.platform.service.PermissionService;
import com.woniuxy.project115.kaer.platform.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.project115.kaer.platform.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fyy
 * @since 2025年02月23日
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RoleMapper roleMapper;
    @Override
    public void addRolePermission(Integer roleId, Integer permissionId) throws Exception {
        //判断角色是否存在
        final QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id",roleId);
        Role role = roleMapper.selectOne(roleQueryWrapper);
        if(role==null){
            throw new Exception("没有该角色，请检查角色名称");
        }
        //查看是否有该资源
        final QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.eq("id",permissionId);
        Permission permission = permissionMapper.selectOne(permissionQueryWrapper);
        if(permission==null){
            throw new Exception("没有该资源，请检查资源名称");
        }
        //查看是否有该权限
        final QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",roleId).eq("permission_id",permissionId);
        if(rolePermissionMapper.selectOne(rolePermissionQueryWrapper)!=null){
            throw new Exception("该角色已经拥有该权限");
        }
        //为该角色添加权限
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permissionId);
        rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public void removeRolePermission(Integer roleId, Integer permissionId) throws Exception {
        //判断角色是否存在
        final QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id",roleId);
        Role role = roleMapper.selectOne(roleQueryWrapper);
        if(role==null){
            throw new Exception("没有该角色，请检查角色名称");
        }
        //查看是否有该资源
        final QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.eq("id",permissionId);
        Permission permission = permissionMapper.selectOne(permissionQueryWrapper);
        if(permission==null){
            throw new Exception("没有该资源，请检查资源名称");
        }
        //查看是否有该权限
        final QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",roleId).eq("permission_id",permissionId);
        RolePermission rolePermission = rolePermissionMapper.selectOne(rolePermissionQueryWrapper);
        if(rolePermission==null){
            throw new Exception("该角色没有该权限");
        }
        // 删除该权限
        rolePermissionMapper.deleteById(rolePermission.getId());
    }

    @Override
    public List<Permission> getRolePermission(Integer roleId) throws Exception {
        //判断角色是否存在
        final QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id",roleId);
        Role role = roleMapper.selectOne(roleQueryWrapper);
        if(role==null){
            throw new Exception("没有该角色，请检查角色名称");
        }
        //查看该角色拥有的所有权限
        final QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        //创建一个null集合，存储权限
        final ArrayList<Permission> list = new ArrayList<>();
        rolePermissionQueryWrapper.eq("role_id",roleId);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(rolePermissionQueryWrapper);
        if(rolePermissions.size()==0||rolePermissions.isEmpty()){
            return list;
        }
        //获取权限列表
        List<Integer> permissionIds = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        final QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.in("id",permissionIds);
        List<Permission> permissions = permissionMapper.selectList(permissionQueryWrapper);
        //将权限添加到集合中
        list.addAll(permissions);
        //返回权限列表
        return list;
    }

    @Override
    public void saveRolePermissions(RolePermissionDto rolePermissionDto) throws Exception {
        //获取角色id
        Integer roleId = rolePermissionDto.getRoleId();
        //删除该角色的所有权限
        final QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",roleId);
        rolePermissionMapper.delete(rolePermissionQueryWrapper);
        //为角色添加新的权限
        List<Integer> permissionIds = rolePermissionDto.getPermissionIds();
        for(Integer permissionId:permissionIds){
            //判断资源是菜单还是权限
            final QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
            permissionQueryWrapper.eq("id",permissionId);
            Permission permission = permissionMapper.selectOne(permissionQueryWrapper);
            if(permission.getParentId()!=0){
                final RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermission.setCreatedTime(LocalDateTime.now());
                rolePermissionMapper.insert(rolePermission);
            }

        }
    }
}
